package com.gamejelly.broker.config;

import com.gamejelly.broker.client.BrokerClient;
import com.gamejelly.broker.common.Handler;
import com.gamejelly.broker.common.handler.DefaultHandler;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "broker.client", value = {"name", "addr"})
public class BrokerClientConfiguration  {

    @Value("${broker.client.name}")
    @Setter
    String name;

    @Value("${broker.client.addr}")
    @Setter
    String addr;

    @Bean
    public DefaultHandler handler() {
        return new DefaultHandler();
    }


    @Bean(initMethod = "init")
    public BrokerClient brokerClient(Handler handler) {
        return BrokerClient.builder()
                .clientName(name)
                .brokerAddr(addr)
                .Build().addRpcHandler(handler);
    }
}
